package com.educate.device.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import com.educate.device.dao.DeviceStatusDao;
import com.educate.device.model.bo.DeviceStatusBO;
import com.educate.device.model.po.DeviceStatus;
import com.educate.device.rpc.api.DeviceStatusService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class DeviceStatusServiceImpl implements DeviceStatusService {

    @Resource DeviceStatusDao deviceStatusDao;

    @Override
    public void create(DeviceStatusBO bo) {
        deviceStatusDao.insert(bo);
    }

    @Override
    public void delete(DeviceStatusBO bo) {
        deviceStatusDao.delete(bo);
    }

    @Override
    public void delete(String deviceStatusId) {
        deviceStatusDao.deleteById(deviceStatusId);
    }

    @Override
    public DeviceStatusBO detail(String deviceStatusId) {
        Optional<DeviceStatus> optional = deviceStatusDao.findById(deviceStatusId);
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        return new DeviceStatusBO(optional.get());
    }

    @Override
    public DeviceStatusBO detail(DeviceStatusBO bo) {
//        return detail(bo.getOrganizationId());
        return null;
    }

    @Override
    public void update(DeviceStatusBO bo) {
        Optional<DeviceStatus> optional = deviceStatusDao.findById(bo.getDeviceId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
        deviceStatusDao.save(optional.get());
    }

    @Override
    public Page<DeviceStatusBO> query(DeviceStatusBO bo, PageRequest pageRequest) {
        return deviceStatusDao.query(bo, pageRequest).map(DeviceStatusBO::new);
    }

    @Override
    public List<DeviceStatusBO> list(DeviceStatusBO bo) {
        List<DeviceStatus> result = deviceStatusDao.findAll();
        return ListUtils.map(result, DeviceStatusBO::new);
    }
}
